package it.allerj.analysis;

import blobDetection.Blob;
import blobDetection.BlobDetection;
import java.awt.image.BufferedImage;

class MarkerDetector extends Detector {
    
    private float circularityTolerance;
        
    MarkerDetector(BufferedImage image, BlobFilter blobFilter, 
            float circTolerance) {
        super(image, blobFilter);
        circularityTolerance = circTolerance;
    }

    @Override
    protected boolean checkBlobDetection( BlobDetection bd ) {

        // retrieve blobs info store them
        for ( int i = 0; i < bd.getBlobNb(); ++i) {
            
            Blob blob = bd.getBlob(i);                        
            DotMark tmp = new DotMark(blob, image_w, image_h);
            
            if (!isCircle(tmp)) {
                continue;
            }
            
            dotMarksFound.add(tmp);
        }
        
        return (dotMarksFound.size() > 0);
    }
    
    /**
    * check if the actual polygon area is similar to the one of the circle 
    * inscribed into the blob bounding box
    * 
    * @return if blob is similar to a circle
    */
    private boolean isCircle(DotMark d) {
        float area = d.getArea();
        float insRadius = d.getDiameter() / 2;
        float insArea = (float) (Math.pow(insRadius, 2) * Math.PI);
        //System.out.println("circle area: "+insArea+", poly area: "+area);
        float ratio = (float) Math.abs(insArea / area);
        
        return (ratio>1-circularityTolerance) && (ratio<1+circularityTolerance);
    }
}
